const path = require('path')
const resolve = (dir) => path.join(__dirname, dir)
const isTong = process.env.NODE_ENV === 'tong'

const CompressionPlugin = require('compression-webpack-plugin')
const webpack = require('webpack')

let ksiot = ''
if (isTong) {
    ksiot = ''
}
const version = new Date().getTime()

// const MiniCssExtractPlugin = require("mini-css-extract-plugin");
// const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
// 环境变量的引入
// const isAnalyzeMode = !!process.env.ANALYZE_MODE
const cdn = {
    js: process.env.PRODUCTION ? [
        {name: 'vue', src: ksiot + '/static/vue.min.js'},
        {name: 'vue-router', src: ksiot + '/static/vue-router.min.js'},
        {name: 'vuex', src: ksiot + '/static/vuex.min.js'},
        {name: 'axios', src: ksiot + '/static/axios.min.js'},
        {name: 'g2', src: ksiot + '/static/g2.min.js'},
        {name: 'swiper', src: ksiot + '/static/swiper.min.js'},
        {name: 'vue-cookies', src: ksiot + '/static/vue-cookies.js'},
        {name: 'echarts', src: ksiot + '/static/echarts.min.js'},
        {name: 'Vant', src: ksiot + '/static/vant.min.js'},
        {name: 'ViewUI', src: ksiot + '/static/iview.min.js'},
        // {name: 'dataV', src: ksiot + '/static/datav.min.vue.js'},
        {name: 'moment', src: ksiot + '/static/moment-with-locales.min.js'}
        // {name: 'Mui', src: ksiot + '/static/mui.min.js'},
    ] : [],
    css: [
        {name: 'iconfont', src: ksiot + '/fonts/common/iconfont.css'},
        {name: 'menuIcon', src: ksiot + '/fonts/menu/iconfont.css'},
        {name: 'swiper', src: ksiot + '/static/swiper.min.css'},
        {name: 'iviewUI1', src: ksiot + '/static/iview3.5.css'},
        {name: 'iviewUI', src: ksiot + '/static/iview4.7.css'},
        {name: 'viewer', src: ksiot + '/static/viewer.css'}
        // {name: 'muiUI', src: ksiot + '/static/mui.min.css'},
    ]
}

module.exports = {
    css: {
        loaderOptions: {
            less: {
                lessOptions: {
                    javascriptEnabled: true
                }
            }
        }
    },
    // 基本路径
    publicPath: ksiot + '/',
    // 输出文件目录
    outputDir: 'mes',
    // eslint-loader 是否在保存的时候检查
    lintOnSave: false,
    productionSourceMap: false,
    chainWebpack: (config) => {
        // 解决view-design在ie空白 按需引入babel转换问题
        config.module
            .rule('view-design') //
            .test(/view-design.src.*?js$/)
            .use('babel')
            .loader('babel-loader')
            .end()

        config.resolve.alias
            .set('images', resolve('src/assets/images'))
            .set('@', resolve('src'))
        config.when(process.title.indexOf('report') > 0, (config) => {
            config
                .plugin('webpack-bundle-analyzer')
                .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
        })
        config.plugin('html').tap((args) => {
            args[0].cdn = cdn
            return args
        })
        // 配置 externals 防止将某些 import 的包(package)打包到 bundle 中，而是在运行时(runtime)再去从外部获取这些扩展依赖
        config.when(process.env.PRODUCTION, config => {
            config.externals({
                vue: 'Vue',
                'vue-router': 'VueRouter',
                vuex: 'Vuex',
                echarts: 'echarts',
                'vue-cookies': '$cookies',
                swiper: 'Swiper',
                '@antv/g2': 'G2',
                axios: 'axios',
                'view-design': 'iview',
                iview: 'iview',
                vant: 'Vant',
                // '@jiaminghi/data-view':'data-view',
                moment: 'moment'
                // 'vue-awesome-mui':'mui',
            })
        })

        config.output.filename(`static/js/[hash].[name].${version}.js`).end()
        config.output
            .chunkFilename('static/js/[hash].[name].' + version + '.js')
            .end()

        config.module
            .rule('images')
            .use('url-loader')
            .tap((options) => {
                options.name = `static/img/[hash].[name].${version}.[ext]`
                options.fallback = {
                    loader: 'file-loader',
                    options: {
                        name: `static/img/[hash].[name].${version}.[ext]`
                    }
                }
                return options
            })
    },
    // webpack-dev-server 相关配置
    devServer: {
        headers: {
            'Access-Control-Allow-Origin': '*'
        },
        disableHostCheck: true,
        proxy: {
            '/basic': {
                // target: "http://121.228.40.79:90/",//生产
                target: 'http://124.220.237.204:90/',
                changeOrigin: true,
                ws: true,
                pathRewrite: {
                    '^/basic': '/basic'
                    // '^//basic': '/basic'
                }
            },
            '/dms': {
                // target: "http://121.228.40.79:90/",//生产
                target: 'http://124.220.237.204:90/',
                changeOrigin: true,
                ws: true,
                pathRewrite: {
                    // '^//dms': '/dms'
                    '^/dms': '/dms'
                }
            },
            // 设置代理
            '/fileServer/': {
                target: 'http://121.228.40.79:90/', // 生产
                // target: "http://124.220.237.204:90/",
                changeOrigin: true,
                ws: true,
                pathRequiresRewrite: {
                    '^/fileServer/': '/fileServer/'
                }
            },
            // 设置代理
            '/saleGoodsManage/static': {
                target: 'http://localhost:9527',
                // target: "https://txqa.ziyun-cloud.com",
                changeOrigin: true,
                ws: true,
                pathRewrite: {
                    '^/saleGoodsManage/static': '/saleGoodsManage/static'
                }
            },
            '/supplyx-mes': {
                target: 'http://114.216.7.53:8088/', // 联刚新测试环境-本地
                changeOrigin: true,
                pathRewrite: {
                    '^/supplyx-mes': '/supplyx-mes'
                }
            },

            // 设置代理
            '/static': {
                // target: "http://121.228.40.79:90/",//生产
                target: 'http://124.220.237.204:90/',
                changeOrigin: true,
                ws: true,
                pathRewrite: {
                    '^/static': '/static'
                }
            },
            '/api': {
                // target: "http://121.228.40.79:90/",//生产
                target: 'http://114.216.7.53:90/',
                // target: 'http://114.216.7.53:90/', // 测试
                changeOrigin: true,
                ws: true,
                pathRewrite: {
                    '^/api': '/'
                }
            }

        },
        before: (app) => {
        }
    },
    // 第三方插件配置
    pluginOptions: {},
    configureWebpack: (config) => {
        return {
            /* plugins: [
                                        new CompressionPlugin({
                                            algorithm: "gzip",
                                            test: new RegExp("\\.(" + ["js", "css"].join("|") + ")$"),
                                            threshold: 10240,
                                            minRatio: 0.8
                                        }),
                                        new webpack.optimize.LimitChunkCountPlugin({
                                            maxChunks: 5,
                                            minChunkSize: 100
                                        })
                                    ]*/
        }
        // if (isAnalyzeMode) {
        //     config.plugins.push(
        //         new BundleAnalyzerPlugin({
        //             // 生成静态文件
        //             analyzerMode: 'static',
        //         })
        //     )
        // }
        // config.plugins.push(
        //     new CompressionWebpackPlugin({
        //         test: /\.(js|css)?$/i, // 压缩文件格式 html
        //         filename: '[path].gz[query]', // 压缩后的文件名
        //         algorithm: 'gzip', // 使用gzip压缩
        //         // 只有大小大于该值的资源会被处理。单位是 bytes。默认值是 0。
        //         // threshold: 10240,
        //         minRatio: 0.8, // 压缩率小于1才会压缩
        //         deleteOriginalAssets: true // 是否删除原资源
        //     })
        // )
    }
}
